package com.tony.pmp.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Joiner;
import com.tony.pmp.model.entity.SysUserPostEntity;
import com.tony.pmp.model.mapper.SysUserPostMapper;
import com.tony.pmp.server.service.SysUserPostService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: SysUserPostServiceImpl
 * @Auther: 皮蛋布丁
 * @Date: 2021/07/26/11:40
 * @Description:
 */
@Service("sysUserPostService")
public class SysUserPostServiceImpl extends ServiceImpl<SysUserPostMapper, SysUserPostEntity> implements SysUserPostService {

    /**
    * @Description: getPostNameByUserId 根据id获取用户岗位信息
    * @Param: [userId]
    * @return: java.lang.String
    * @Author: 皮蛋布丁
    * @Date: 2021/7/26 11:44
    */
    @Override
    public String getPostNameByUserId(Long userId) {
        //region 第一种写法：使用谷歌插件

        /*Set<String> set = baseMapper.getPostNameByUserId(userId);
        if (set != null && !set.isEmpty()) {
            String qq = Joiner.on(",").join(set);
            return qq;
        } else {
            return "";
        }*/

        //endregion

        //region 第二种写法：传统写法

        /*StringBuilder sb = new StringBuilder("");
        List<SysUserPostEntity> list = baseMapper.getByUserId(userId);
        if (list != null && !list.isEmpty()) {
            for (SysUserPostEntity entity : list) {
                sb.append(entity.getPostName()).append(",");
            }
        }
        String result = sb.toString();
        if (result.lastIndexOf(",") >= 0) {
            result = result.substring(0,result.lastIndexOf(","));
        }
        return result;*/

        //endregion

        //region 第三中写法：java8的stream api

        String result = "";
        List<SysUserPostEntity> list = baseMapper.getByUserId(userId);
        if (list != null && !list.isEmpty()) {
            Set<String> set = list.stream().map(SysUserPostEntity::getPostName).collect(Collectors.toSet());
            result = Joiner.on(",").join(set);
        }
        return result;

        //endregion
    }

    /**
    * @Description: saveOrUpdate 新增用户岗位信息
    * @Param: [userId, postIds]
    * @return: boolean
    * @Author: 皮蛋布丁
    * @Date: 2021/7/26 14:46
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(Long userId, List<Long> postIds) {
        //删除旧数据
        this.remove(new QueryWrapper<SysUserPostEntity>().eq("user_id",userId));

        if (postIds != null && ! postIds.isEmpty()) {
            SysUserPostEntity entity;
            for (Long postId : postIds) {
                entity = new SysUserPostEntity();
                entity.setPostId(postId);
                entity.setUserId(userId);
                this.save(entity);
            }
        }
        return true;
    }

    /**
    * @Description: queryPostIdList 根据userId获取用户岗位id信息
    * @Param: [userId]
    * @return: java.util.List<java.lang.Long>
    * @Author: 皮蛋布丁
    * @Date: 2021/7/26 21:30
    */
    @Override
    public List<Long> queryPostIdList(Long userId) {
        return baseMapper.queryPostIdList(userId);
    }
}
